package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyFavors;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVo;
import cn.wolfcode.wolf2w.redis.vo.TravelStatisVo;
import cn.wolfcode.wolf2w.service.IStrategyFavorsService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 监听缓存数据初始化，将数据库中vo、用户攻略集合list初始化到redis缓存中
 */
 @Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{
    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IStrategyFavorsService strategyFavorsService;

    @Autowired
    private ITravelService travelService;

    @Autowired
    private IUserInfoService userInfoService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        // 1、查询所有的攻略对象vo
        List<Strategy> list = strategyService.listAll();
        // 2、遍历，封装好vo对象，并设置到redis中；如果redis中已经存在，就不需要再初始化（否则会从数据库中重新获取并覆盖之前的数据）
        for (Strategy strategy : list) {
            // 拼接key
            String key = RedisKeys.STRATEGY_STATIS_VO.join(strategy.getId());
            if (redisTemplate.hasKey(key)){
                continue;
            }
            StrategyStatisVo vo = new StrategyStatisVo();
            BeanUtils.copyProperties(strategy,vo);
            vo.setStrategyId(strategy.getId());
            redisTemplate.opsForValue().set(key, JSON.toJSONString(vo));
        }

        // 1、查询所有的用户攻略集合list
        List<StrategyFavors> favorsList = strategyFavorsService.listAll();
        // 2、遍历，获取每一个用户攻略集合list，并设置到redis中；如果redis中已经存在，就不需要再初始化（否则会从数据库中重新获取并覆盖之前的数据）
        for (StrategyFavors strategyFavors : favorsList) {
            // 拼接key
            String key = RedisKeys.USER_STRATEGY_FAVOR.join(strategyFavors.getUsid());
            if (redisTemplate.hasKey(key)){
                continue;
            }
            redisTemplate.opsForValue().set(key,JSON.toJSONString(strategyFavors.getFavors()));
        }

        // 初始化 游记vo
        List<Travel> travels = travelService.listAll();
        for (Travel travel : travels) {
            String key = RedisKeys.TRAVEL_STATIS_VO.join(travel.getId());
            if (redisTemplate.hasKey(key)){
                continue;
            }
            TravelStatisVo vo = new TravelStatisVo();
            BeanUtils.copyProperties(travel,vo);
            vo.setTravelId(travel.getId());
            redisTemplate.opsForValue().set(key,JSON.toJSONString(vo));
        }

        // 初始化 用户收藏游记的list
        List<UserInfo> userInfos = userInfoService.listAll();
        for (UserInfo userInfo : userInfos) {
            String key = RedisKeys.USER_TRAVEL_FAVOR.join(userInfo.getId());
            if (redisTemplate.hasKey(key)){
                continue;
            }
            List<String> travelFavors = userInfo.getTravelFavors();
            redisTemplate.opsForValue().set(key,JSON.toJSONString(travelFavors));
        }
    }
}
